// DialogEditPWD.cpp : implementation file
//

#include "stdafx.h"
#include "ECGWorkStation.h"
#include "DialogEditPWD.h"
#include "..\public\BiocareDB\BiocareDB.h"
#include "..\Public\pubfun\inireadwrite\inireadwrite.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDialogEditPWD dialog


CDialogEditPWD::CDialogEditPWD(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogEditPWD::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialogEditPWD)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDialogEditPWD::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialogEditPWD)
	DDX_Control(pDX, IDC_EDIT_OLDPWD, m_edtOldPWD);
	DDX_Control(pDX, IDC_EDIT_NEWPWD, m_edtNewPWD);
	DDX_Control(pDX, IDC_EDIT_NEWCONFIRM, m_edtNewPWDConfirm);
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialogEditPWD, CDialog)
	//{{AFX_MSG_MAP(CDialogEditPWD)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogEditPWD message handlers

void CDialogEditPWD::OnOK() 
{
	CString strErrorMsg = "";
	CString strNewPWD = "", strNewPWDConfirm = "", strOldPWD = "";
	m_edtOldPWD.GetWindowText(strOldPWD);
	m_edtNewPWD.GetWindowText(strNewPWD);
	m_edtNewPWDConfirm.GetWindowText(strNewPWDConfirm);
	if(!IsTrueOldPWD(strOldPWD)){
		strErrorMsg.LoadString(IDS_STRING121);
		MessageBox(strErrorMsg, NULL, MB_OKCANCEL|MB_ICONERROR);

		return;
	}

	if(strNewPWD != strNewPWDConfirm){
		strErrorMsg.LoadString(IDS_STRING114);
		MessageBox(strErrorMsg, NULL, MB_OKCANCEL|MB_ICONERROR);

		return;
	}

	char szErrorMsg[1024];
	memset(szErrorMsg, 0, 1024);

	BOOL bRet = FALSE;
	BCDBConnection DBConn;
	if(!BCDBConnectDB(&DBConn, (LPCTSTR)theApp.m_strConnection, (LPCTSTR)theApp.m_strConnUserName, (LPCTSTR)theApp.m_strConnPassWord, szErrorMsg)) return;
	BCDBDataSet* pDataSet = NULL;
	BCDBCreateDataSet(&pDataSet, szErrorMsg);
	BCDBDataElem* pDataElem = NULL;
	BCDBCreateDataElem(&pDataElem, T_USER_PASSWORD, strNewPWD.GetLength(), DT_STRING, (void*)(LPCTSTR)strNewPWD, szErrorMsg);
	pDataSet->push_back(pDataElem); pDataElem = NULL;
	bRet = BCDBUpdateDBData(&DBConn, T_USER, (LPCTSTR)theApp.m_strUserID, pDataSet, szErrorMsg);
	bRet = BCDBDisconnectionDB(&DBConn, szErrorMsg);

	BCDBClearDataSet(pDataSet, szErrorMsg);

	CDialog::OnOK();
}

BOOL CDialogEditPWD::IsTrueOldPWD(CString strOldPWD)
{
	char szErrorMsg[1024];
	memset(szErrorMsg, 0, 1024);

	BOOL bRet = FALSE;
	BCDBConnection DBConn;
	if(!BCDBConnectDB(&DBConn, (LPCTSTR)theApp.m_strConnection, (LPCTSTR)theApp.m_strConnUserName, (LPCTSTR)theApp.m_strConnPassWord, szErrorMsg)) return FALSE;
	BCDBDataSet* pDataSet = NULL;
	BCDBCreateDataSet(&pDataSet, szErrorMsg);
	BCDBDataSets* pDataSets = NULL;
	BCDBCreateDataSets(&pDataSets, szErrorMsg);

	BCDBDataElem* pDataElem = NULL;
	BCDBCreateDataElem(&pDataElem, T_USER_ID, theApp.m_strUserID.GetLength(), DT_STRING, (void*)(LPCTSTR)theApp.m_strUserID, szErrorMsg);
	pDataSet->push_back(pDataElem); pDataElem = NULL;
	if(!BCDBQueryDBData(&DBConn, T_USER, pDataSet, &pDataSets, szErrorMsg)){
		BCDBDisconnectionDB(&DBConn, szErrorMsg);
		BCDBClearDataSet(pDataSet, szErrorMsg);
		BCDBClearDataSets(pDataSets, szErrorMsg);

		return FALSE;
	}
	BCDBDisconnectionDB(&DBConn, szErrorMsg);

	BCDBDataSets::iterator itDataSets = pDataSets->begin();
	if (itDataSets == pDataSets->end()) {
		BCDBClearDataSet(pDataSet, szErrorMsg);
		BCDBClearDataSets(pDataSets, szErrorMsg);

		return FALSE;
	}

	BCDBDataSet* pDataSetResult = *itDataSets;
	BCDBDataSet::iterator itDataSet;
	for (itDataSet = pDataSetResult->begin(); itDataSet != pDataSetResult->end(); ++itDataSet)
	{
		pDataElem = *itDataSet;
		if (!strncmp(pDataElem->szName, T_USER_PASSWORD, strlen(T_USER_PASSWORD))) {
			if (!strcmp(strOldPWD, (const char*)(pDataElem->pValue))) {
				BCDBClearDataSet(pDataSet, szErrorMsg);
				BCDBClearDataSets(pDataSets, szErrorMsg);

				return TRUE;
			}
			break;
		}

		pDataElem = NULL;
	}

	BCDBClearDataSet(pDataSet, szErrorMsg);
	BCDBClearDataSets(pDataSets, szErrorMsg);

	return FALSE;
}
